package com.nsu.cl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsu.cl.pojo.LoginForm;
import com.nsu.cl.pojo.Teacher;
import com.nsu.cl.service.TeacherService;
import com.nsu.cl.mapper.TeacherMapper;
import com.nsu.cl.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * @author 52618
 * @description 针对表【teacher(老师表)】的数据库操作Service实现
 * @createDate 2023-02-16 14:21:02
 */
@Service
@Transactional
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
        implements TeacherService {

    @Override
    public Teacher login(LoginForm loginForm) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", loginForm.getUsername());
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword()));
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long userId) {

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<Teacher> getTeachersByOpr(Page<Teacher> pageParam, Teacher teacher) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacher.getName())) {
            queryWrapper.like("name", teacher.getName());
        }
        if (!StringUtils.isEmpty(teacher.getGradeName())) {
            queryWrapper.like("grade_name", teacher.getGradeName());
        }
        queryWrapper.orderByAsc("id");

        Page<Teacher> page = baseMapper.selectPage(pageParam, queryWrapper);

        return page;
    }

    @Override
    public List<Teacher> getTeacherByGradeName(Teacher teacher) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(teacher.getGradeName())) {
            queryWrapper.eq("grade_name", teacher.getGradeName());
        }
        if (!StringUtils.isEmpty(teacher.getSubjectName())) {
            queryWrapper.eq("subject_name", teacher.getSubjectName());
        }
        List<Teacher> teacherList = baseMapper.selectList(queryWrapper);
        return teacherList;
    }
}




